package com.project.lk.nurseapp.nurse.model;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.project.lk.nurseapp.base.BaseApi;
import com.project.lk.nurseapp.base.PostBaseModel;
import com.project.lk.nurseapp.home.usermsg.model.PostFileHintBean;
import com.project.lk.nurseapp.utils.JsonUtils;
import com.project.lk.nurseapp.utils.ToastUtils;

import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class PostFileModel extends PostBaseModel {

    private String mType;

    public void postLoadJson(String url, String path, String sendType){
        mType = sendType;
        Log.e("MLOG",sendType+"sendType");
        Random random = new Random();
        int nextInt = random.nextInt();
        File file = new File(path);
        if (file!=null) {
            MultipartBody.Builder builder = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM);
            RequestBody body= RequestBody.create(MediaType.parse("*/*"),file );
            long currentTimeMillis = System.currentTimeMillis();
            builder.addFormDataPart("filePath", path, body)
                    .addFormDataPart("signature",postSignToken(currentTimeMillis+"",nextInt+""))
                    .addFormDataPart("timestamp",currentTimeMillis+"")
                    .addFormDataPart("nonce",nextInt+"");
            getJsonData(url,builder.build());
        }
    }
    @Override
    public List<PostFileHintBean> analyzeJson(String json, String url) {
        Log.e("MLOG",url+json);

        try {
            JSONObject jsonObject=new JSONObject(json);
            String msg = jsonObject.getString("msg");
            String info = jsonObject.getString("info");
            if (TextUtils.equals(msg,"success")){
                JSONObject data = jsonObject.getJSONObject("data");
                List<PostFileHintBean> list=new ArrayList<>();
                PostFileHintBean bean = (PostFileHintBean) JsonUtils.getJsonUtils()
                        .toObject(new TypeToken<PostFileHintBean>(){}.getType(),data.toString());
                list.add(bean);
                ToastUtils.showShortToast(info);
                return list;
            }else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("MLOG",e.getMessage());
            return null;
        }
    }


    public void postImgSuccess(){
        bridge.bridge(BaseApi.img_success,null);
    }
    public void postImgFail(){
        bridge.bridge(BaseApi.img_fail,null);
    }

    public void postVideoSuccess(){
        bridge.bridge(BaseApi.video_success,null);
    }
    public void postVideoFail(){
        bridge.bridge(BaseApi.video_fail,null);
    }

    public void postAudioSuccess(){
        bridge.bridge(BaseApi.audio_success,null);
    }
    public void postAudioFail(){
        bridge.bridge(BaseApi.audio_fail,null);
    }


    List<PostFileHintBean> mImgList=new ArrayList<>();

    List<PostFileHintBean> mVideoList=new ArrayList<>();
    List<PostFileHintBean> mAudioList=new ArrayList<>();
    public List<PostFileHintBean> getImgList() {
        return mImgList;
    }
    public List<PostFileHintBean> getVideoList() {
        return mVideoList;
    }
    public List<PostFileHintBean> getAudioList() {
        return mAudioList;
    }
    @Override
    public void postSuccessOnMainThread(List list, String url) {

        switch (mType){
            case BaseApi.image_type:
                if (list!=null&&list.size()!=0){
                    mImgList.clear();
                    mImgList.addAll(list);
                    postImgSuccess();
                }else {
                    postImgFail();
                }
                break;
            case BaseApi.audio_type:
                if (list!=null&&list.size()!=0){
                    mAudioList.clear();
                    mAudioList.addAll(list);
                    postAudioSuccess();
                }else {
                    postAudioFail();
                }
                break;
            case BaseApi.video_type:
                if (list!=null&&list.size()!=0){
                    mVideoList.clear();
                    mVideoList.addAll(list);
                    postVideoSuccess();
                }else {
                    postVideoFail();
                }
                break;
        }

    }

    @Override
    public void postFailedOnMainThread(String url) {
        switch (mType){
            case BaseApi.image_type:
                postImgFail();
                break;
            case BaseApi.audio_type:
                postAudioFail();
                break;
            case BaseApi.video_type:
                postVideoFail();
                break;
        }
    }
}
